﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace XUtliPoolLib
{
	// Token: 0x020001F2 RID: 498
	public class XAnimator
	{
		// Token: 0x06000BBB RID: 3003 RVA: 0x0003CBB8 File Offset: 0x0003ADB8
		public XAnimator()
		{
			if (XAnimator.loadCallbacks == null)
			{
				XAnimator.loadCallbacks = new AnimLoadCallback[]
				{
					XAnimator.SyncEnableCmd,
					XAnimator.SyncSpeedCmd,
					XAnimator.SyncTriggerCmd,
					XAnimator.SyncSetFloatCmd
				};
			}
		}

		// Token: 0x170000E9 RID: 233
		// (get) Token: 0x06000BBC RID: 3004 RVA: 0x0003CC98 File Offset: 0x0003AE98
		public bool IsLoaded
		{
			get
			{
				return this.m_LoadStatus == 2;
			}
		}

		// Token: 0x170000EA RID: 234
		// (get) Token: 0x06000BBD RID: 3005 RVA: 0x0003CCB4 File Offset: 0x0003AEB4
		private bool IsInLoadFinish
		{
			get
			{
				return this.m_LoadStatus == 1;
			}
		}

		// Token: 0x170000EB RID: 235
		// (get) Token: 0x06000BBE RID: 3006 RVA: 0x0003CCD0 File Offset: 0x0003AED0
		// (set) Token: 0x06000BBF RID: 3007 RVA: 0x0003CCE8 File Offset: 0x0003AEE8
		public float speed
		{
			get
			{
				return this.m_speed;
			}
			set
			{
				this.m_speed = value;
				if (this.IsLoaded)
				{
					XAnimator.SyncSpeed(this);
				}
				else
				{
					this.SetCbFlag(XAnimator.ECallbackCmd.ESyncSpeed, true);
				}
			}
		}

		// Token: 0x170000EC RID: 236
		// (get) Token: 0x06000BC0 RID: 3008 RVA: 0x0003CD1C File Offset: 0x0003AF1C
		// (set) Token: 0x06000BC1 RID: 3009 RVA: 0x0003CD34 File Offset: 0x0003AF34
		public bool enabled
		{
			get
			{
				return this.m_enable;
			}
			set
			{
				this.m_enable = value;
				if (this.IsLoaded)
				{
					XAnimator.SyncEnable(this);
				}
				else
				{
					this.SetCbFlag(XAnimator.ECallbackCmd.ESyncEnable, true);
				}
			}
		}

		// Token: 0x170000ED RID: 237
		// (get) Token: 0x06000BC2 RID: 3010 RVA: 0x0003CD68 File Offset: 0x0003AF68
		public string StateName
		{
			get
			{
				return this.m_stateName;
			}
		}

		// Token: 0x170000EE RID: 238
		// (set) Token: 0x06000BC3 RID: 3011 RVA: 0x0003CD80 File Offset: 0x0003AF80
		public AnimatorCullingMode cullingMode
		{
			set
			{
				this.m_cullingMode = value;
				if (this.IsLoaded)
				{
					XAnimator.SyncAnimationCullingMode(this);
				}
			}
		}

		// Token: 0x06000BC4 RID: 3012 RVA: 0x0003CDA8 File Offset: 0x0003AFA8
		private void SetCbFlag(XAnimator.ECallbackCmd cmd, bool add)
		{
			int num = XFastEnumIntEqualityComparer<XAnimator.ECallbackCmd>.ToInt(cmd);
			if (add)
			{
				this.m_LoadFinishCbFlag |= num;
			}
			else
			{
				this.m_LoadFinishCbFlag &= ~num;
			}
		}

		// Token: 0x06000BC5 RID: 3013 RVA: 0x0003CDE8 File Offset: 0x0003AFE8
		private bool IsCbFlag(int index)
		{
			int num = 1 << index;
			return (this.m_LoadFinishCbFlag & num) != 0;
		}

		// Token: 0x06000BC6 RID: 3014 RVA: 0x0003CE0C File Offset: 0x0003B00C
		private void CacheFloatValue(string name, float value)
		{
			if (this.m_FloatCache == null)
			{
				this.m_FloatCache = DictionaryPool<string, float>.Get();
			}
			this.m_FloatCache[name] = value;
		}

		// Token: 0x06000BC7 RID: 3015 RVA: 0x0003CE44 File Offset: 0x0003B044
		public void Init(GameObject gameObject)
		{
			this.m_LoadStatus = 1;
			this.m_Ator = ((gameObject != null) ? gameObject.GetComponent<Animator>() : null);
			if (this.m_Ator != null)
			{
				this.m_Ator.enabled = true;
				if (this.m_Ator.runtimeAnimatorController is AnimatorOverrideController)
				{
					this.m_overrideController = (this.m_Ator.runtimeAnimatorController as AnimatorOverrideController);
				}
				else
				{
					this.m_overrideController = new AnimatorOverrideController();
					this.m_overrideController.runtimeAnimatorController = this.m_Ator.runtimeAnimatorController;
					this.m_Ator.runtimeAnimatorController = this.m_overrideController;
				}
				foreach (KeyValuePair<string, XAnimator.AminInfo> keyValuePair in this.m_Clips)
				{
					XAnimator.AminInfo value = keyValuePair.Value;
					if (value.xclip != null)
					{
						this.SyncOverrideAnim(value.clipKey, value.xclip);
					}
				}
				this.m_Ator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
				this.m_Ator.Rebind();
				for (int i = 0; i < XAnimator.loadCallbacks.Length; i++)
				{
					if (this.IsCbFlag(i))
					{
						AnimLoadCallback animLoadCallback = XAnimator.loadCallbacks[i];
						animLoadCallback(this);
					}
				}
			}
			this.m_LoadFinishCbFlag = 0;
			this.m_LoadStatus = 2;
		}

		// Token: 0x06000BC8 RID: 3016 RVA: 0x0003CFB8 File Offset: 0x0003B1B8
		public bool IsSame(Animator ator)
		{
			return this.m_Ator == ator;
		}

		// Token: 0x06000BC9 RID: 3017 RVA: 0x0003CFD8 File Offset: 0x0003B1D8
		public bool IsAnimStateValid()
		{
			bool result;
			if (!this.IsLoaded)
			{
				result = false;
			}
			else
			{
				result = !string.IsNullOrEmpty(this.m_stateName);
			}
			return result;
		}

		// Token: 0x06000BCA RID: 3018 RVA: 0x0003D010 File Offset: 0x0003B210
		public void Reset()
		{
			if (this.m_Ator != null)
			{
				this.m_Ator.cullingMode = AnimatorCullingMode.CullUpdateTransforms;
				this.m_Ator.enabled = false;
				this.m_Ator = null;
			}
			if (this.m_overrideController != null)
			{
				foreach (AnimationClipPair animationClipPair in this.m_overrideController.clips)
				{
					if (animationClipPair.overrideClip != null)
					{
						this.m_overrideController[animationClipPair.originalClip.name] = null;
					}
				}
				this.m_overrideController = null;
			}
			foreach (KeyValuePair<string, XAnimator.AminInfo> keyValuePair in this.m_Clips)
			{
				XAnimator.AminInfo value = keyValuePair.Value;
				if (value.xclip != null)
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(value.clipPath, ".anim", value.xclip, false);
				}
			}
			this.m_Clips.Clear();
			this.m_cullingMode = AnimatorCullingMode.CullUpdateTransforms;
			this.m_delayPlay = false;
			this.m_stateName = "";
			this.m_value = -1f;
			this.m_playLayer = -1;
			this.m_normalizedTime = float.NegativeInfinity;
			this.m_triggerName = "";
			this.m_LoadStatus = 0;
			this.m_LoadFinishCbFlag = 0;
			this.xGameObject = null;
		}

		// Token: 0x06000BCB RID: 3019 RVA: 0x00003284 File Offset: 0x00001484
		public void SetStateMachine(IAnimStateMachine stateMachine)
		{
		}

		// Token: 0x06000BCC RID: 3020 RVA: 0x0003D18C File Offset: 0x0003B38C
		private void SyncOverrideAnim(string key, XAnimationClip xclip)
		{
			if (this.m_Ator != null)
			{
				if (XAnimator.debug)
				{
					XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} OverrideAnim {1}", new object[]
					{
						this.xGameObject.Name,
						key
					});
				}
				this.m_overrideController[key] = ((xclip != null) ? xclip.clip : null);
			}
		}

		// Token: 0x06000BCD RID: 3021 RVA: 0x0003D1FC File Offset: 0x0003B3FC
		public void OverrideAnim(string key, string clipPath, OverrideAnimCallback overrideAnim = null, bool forceOverride = false)
		{
			//bool flag = clipPath == null;
			if (clipPath == null)
			{
				clipPath = "";
			}
			//bool flag2 = this.m_Ator == null && this.IsLoaded;
			if (/*!flag2*/this.m_Ator != null || !this.IsLoaded)
			{
				XAnimator.AminInfo aminInfo;
				//bool flag3 = this.m_Clips.TryGetValue(key, out aminInfo);
				if (this.m_Clips.TryGetValue(key, out aminInfo))
				{
					//bool flag4 = aminInfo.clipPath == clipPath;
					if (aminInfo.clipPath == clipPath)
					{
						//bool flag5 = forceOverride && this.IsLoaded;
						if (forceOverride && this.IsLoaded)
						{
							this.SyncOverrideAnim(key, aminInfo.xclip);
						}
						//bool flag6 = overrideAnim != null;
						if (overrideAnim != null)
						{
							overrideAnim(aminInfo.xclip);
						}
						return;
					}
					//bool flag7 = aminInfo.xclip != null;
					if (aminInfo.xclip != null)
					{
						XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource("", ".anim", aminInfo.xclip, false);
						aminInfo.xclip = null;
					}
				}
				else
				{
					aminInfo.clipKey = key;
				}
				aminInfo.clipPath = clipPath;
				aminInfo.xclip = (string.IsNullOrEmpty(clipPath) ? null : XSingleton<XResourceLoaderMgr>.singleton.GetXAnimation(clipPath, true, false));
				this.m_Clips[key] = aminInfo;
				if (this.IsLoaded)
				{
					this.SyncOverrideAnim(key, aminInfo.xclip);
				}
				//bool flag8 = overrideAnim != null;
				if (overrideAnim != null)
				{
					overrideAnim(aminInfo.xclip);
				}
			}
		}

		// Token: 0x06000BCE RID: 3022 RVA: 0x0003D350 File Offset: 0x0003B550
		public void SetAnimLoadCallback(string key, OverrideAnimCallback overrideAnim)
		{
			bool flag = this.m_Ator == null && this.IsLoaded;
			if (!flag)
			{
				if (overrideAnim != null)
				{
					XAnimator.AminInfo aminInfo;
					if (this.m_Clips.TryGetValue(key, out aminInfo))
					{
						if (aminInfo.xclip != null)
						{
							overrideAnim(aminInfo.xclip);
						}
					}
				}
			}
		}

		// Token: 0x06000BCF RID: 3023 RVA: 0x0003D3BC File Offset: 0x0003B5BC
		public static void SyncAnimationCullingMode(XAnimator ator)
		{
			if (ator != null && ator.m_Ator != null)
			{
				ator.m_Ator.cullingMode = ator.m_cullingMode;
			}
		}

		// Token: 0x06000BD0 RID: 3024 RVA: 0x0003D3F4 File Offset: 0x0003B5F4
		private static void SyncTrigger(XAnimator ator)
		{
			//bool flag = ator != null && ator.m_Ator != null && !string.IsNullOrEmpty(ator.m_triggerName);
			if (ator != null && ator.m_Ator != null && !string.IsNullOrEmpty(ator.m_triggerName))
			{
				//bool flag2 = XAnimator.debug;
				if (XAnimator.debug)
				{
					XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} trigger {1}", new object[]
					{
						ator.xGameObject.Name,
						ator.m_triggerName
					});
				}
				ator.m_Ator.SetTrigger(ator.m_triggerName);
			}
		}

		// Token: 0x06000BD1 RID: 3025 RVA: 0x0003D474 File Offset: 0x0003B674
		private static void SyncSetFloat(XAnimator ator)
		{
			if (ator != null && ator.m_Ator != null)
			{
				if (ator.m_FloatCache != null)
				{
					Dictionary<string, float>.Enumerator enumerator = ator.m_FloatCache.GetEnumerator();
					while (enumerator.MoveNext())
					{
						KeyValuePair<string, float> keyValuePair;
						if (XAnimator.debug)
						{
							XDebug singleton = XSingleton<XDebug>.singleton;
							string format = "GO:{0} SetFloat {1}";
							object[] array = new object[2];
							array[0] = ator.xGameObject.Name;
							int num = 1;
							keyValuePair = enumerator.Current;
							array[num] = keyValuePair.Key;
							singleton.AddWarningLog2(format, array);
						}
						Animator ator2 = ator.m_Ator;
						keyValuePair = enumerator.Current;
						string key = keyValuePair.Key;
						keyValuePair = enumerator.Current;
						ator2.SetFloat(key, keyValuePair.Value);
					}
					DictionaryPool<string, float>.Release(ator.m_FloatCache);
					ator.m_FloatCache = null;
				}
			}
		}

		// Token: 0x06000BD2 RID: 3026 RVA: 0x0003D54C File Offset: 0x0003B74C
		private static void SyncSpeed(XAnimator ator)
		{
			if (ator != null && ator.m_Ator != null)
			{
				ator.m_Ator.speed = ator.m_speed;
			}
		}

		// Token: 0x06000BD3 RID: 3027 RVA: 0x0003D584 File Offset: 0x0003B784
		private static void SyncEnable(XAnimator ator)
		{
			if (ator != null && ator.m_Ator != null)
			{
				ator.m_Ator.enabled = ator.m_enable;
			}
		}

		// Token: 0x06000BD4 RID: 3028 RVA: 0x0003D5BC File Offset: 0x0003B7BC
		public static void Update(XAnimator ator)
		{
			XAnimator.SyncAnimationCullingMode(ator);
			if (ator.m_delayPlay)
			{
				ator.RealPlay();
			}
		}

		// Token: 0x06000BD5 RID: 3029 RVA: 0x0003D5E4 File Offset: 0x0003B7E4
		public void RealPlay()
		{
			this.m_delayPlay = false;
			if (this.m_Ator != null)
			{
				if (this.m_crossFade)
				{
					if (XAnimator.debug)
					{
						XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} crossFade {1} {2}", new object[]
						{
							this.xGameObject.Name,
							this.m_stateName,
							this.m_Ator.IsInTransition(0)
						});
					}
					this.m_Ator.CrossFade(this.m_stateName, this.m_value, this.m_playLayer, this.m_normalizedTime);
				}
				else
				{
					if (XAnimator.debug)
					{
						XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} play {1}", new object[]
						{
							this.xGameObject.Name,
							this.m_stateName
						});
					}
					this.m_Ator.Play(this.m_stateName, this.m_playLayer, this.m_normalizedTime);
				}
			}
		}

		// Token: 0x06000BD6 RID: 3030 RVA: 0x0003D6E0 File Offset: 0x0003B8E0
		public void Play(string stateName, int layer, float normalizedTime)
		{
			this.m_stateName = stateName;
			this.m_playLayer = layer;
			this.m_normalizedTime = normalizedTime;
			this.m_crossFade = false;
			if (this.IsAnimStateValid())
			{
				this.RealPlay();
			}
			else
			{
				this.m_delayPlay = true;
			}
		}

		// Token: 0x06000BD7 RID: 3031 RVA: 0x0003D728 File Offset: 0x0003B928
		public void Play(string stateName, int layer)
		{
			this.m_stateName = stateName;
			this.m_playLayer = layer;
			this.m_normalizedTime = float.NegativeInfinity;
			this.m_crossFade = false;
			if (this.IsAnimStateValid())
			{
				this.RealPlay();
			}
			else
			{
				this.m_delayPlay = true;
			}
		}

		// Token: 0x06000BD8 RID: 3032 RVA: 0x0003D774 File Offset: 0x0003B974
		public void CrossFade(string stateName, float transitionDuration, int layer, float normalizedTime)
		{
			this.m_stateName = stateName;
			this.m_value = transitionDuration;
			this.m_playLayer = layer;
			this.m_normalizedTime = normalizedTime;
			this.m_crossFade = true;
			if (this.IsAnimStateValid())
			{
				this.RealPlay();
			}
			else
			{
				this.m_delayPlay = true;
			}
		}

		// Token: 0x06000BD9 RID: 3033 RVA: 0x0003D7C4 File Offset: 0x0003B9C4
		public void SetTrigger(string name)
		{
			this.m_triggerName = name;
			if (this.IsLoaded)
			{
				XAnimator.SyncTrigger(this);
			}
			else
			{
				this.SetCbFlag(XAnimator.ECallbackCmd.ESyncTrigger, true);
			}
		}

		// Token: 0x06000BDA RID: 3034 RVA: 0x0003D7FC File Offset: 0x0003B9FC
		public void ResetTrigger()
		{
			if (this.IsLoaded)
			{
				XAnimator.SyncTrigger(this);
			}
			else
			{
				this.SetCbFlag(XAnimator.ECallbackCmd.ESyncTrigger, true);
			}
		}

		// Token: 0x06000BDB RID: 3035 RVA: 0x0003D82C File Offset: 0x0003BA2C
		public bool IsInTransition(int layerIndex)
		{
			return !(this.m_Ator == null) && this.m_Ator.IsInTransition(layerIndex);
		}

		// Token: 0x06000BDC RID: 3036 RVA: 0x0003D85C File Offset: 0x0003BA5C
		public void SetFloat(string name, float value)
		{
			if (this.IsLoaded)
			{
				if (this.m_Ator != null)
				{
					this.m_Ator.SetFloat(name, value);
				}
			}
			else
			{
				this.CacheFloatValue(name, value);
				this.SetCbFlag(XAnimator.ECallbackCmd.ESyncSetFloat, true);
			}
		}

		// Token: 0x06000BDD RID: 3037 RVA: 0x0003D8AC File Offset: 0x0003BAAC
		public void EnableRootMotion(bool enable)
		{
			if (this.m_Ator != null)
			{
				this.m_Ator.applyRootMotion = enable;
			}
		}

		// Token: 0x040005B4 RID: 1460
		private short m_LoadStatus = 0;

		// Token: 0x040005B5 RID: 1461
		public XGameObject xGameObject = null;

		// Token: 0x040005B6 RID: 1462
		private Animator m_Ator = null;

		// Token: 0x040005B7 RID: 1463
		private AnimatorOverrideController m_overrideController = null;

		// Token: 0x040005B8 RID: 1464
		private AnimatorCullingMode m_cullingMode = AnimatorCullingMode.CullUpdateTransforms;

		// Token: 0x040005B9 RID: 1465
		private bool m_delayPlay = false;

		// Token: 0x040005BA RID: 1466
		private bool m_crossFade = false;

		// Token: 0x040005BB RID: 1467
		private string m_stateName = "";

		// Token: 0x040005BC RID: 1468
		private int m_playLayer = 0;

		// Token: 0x040005BD RID: 1469
		private float m_normalizedTime = float.NegativeInfinity;

		// Token: 0x040005BE RID: 1470
		private float m_value = 0f;

		// Token: 0x040005BF RID: 1471
		private string m_triggerName = "";

		// Token: 0x040005C0 RID: 1472
		private float m_speed = 1f;

		// Token: 0x040005C1 RID: 1473
		private bool m_enable = true;

		// Token: 0x040005C2 RID: 1474
		private Dictionary<string, float> m_FloatCache = null;

		// Token: 0x040005C3 RID: 1475
		private int m_LoadFinishCbFlag = 0;

		// Token: 0x040005C4 RID: 1476
		private static AnimLoadCallback SyncTriggerCmd = new AnimLoadCallback(XAnimator.SyncTrigger);

		// Token: 0x040005C5 RID: 1477
		private static AnimLoadCallback SyncSetFloatCmd = new AnimLoadCallback(XAnimator.SyncSetFloat);

		// Token: 0x040005C6 RID: 1478
		private static AnimLoadCallback SyncSpeedCmd = new AnimLoadCallback(XAnimator.SyncSpeed);

		// Token: 0x040005C7 RID: 1479
		private static AnimLoadCallback SyncEnableCmd = new AnimLoadCallback(XAnimator.SyncEnable);

		// Token: 0x040005C8 RID: 1480
		private static AnimLoadCallback[] loadCallbacks = null;

		// Token: 0x040005C9 RID: 1481
		private Dictionary<string, XAnimator.AminInfo> m_Clips = new Dictionary<string, XAnimator.AminInfo>();

		// Token: 0x040005CA RID: 1482
		public static bool debug = false;

		// Token: 0x020003A6 RID: 934
		private enum ECallbackCmd
		{
			// Token: 0x04001057 RID: 4183
			ESyncEnable = 1,
			// Token: 0x04001058 RID: 4184
			ESyncSpeed,
			// Token: 0x04001059 RID: 4185
			ESyncTrigger = 4,
			// Token: 0x0400105A RID: 4186
			ESyncSetFloat = 8
		}

		// Token: 0x020003A7 RID: 935
		private struct AminInfo
		{
			// Token: 0x0400105B RID: 4187
			public XAnimationClip xclip;

			// Token: 0x0400105C RID: 4188
			public string clipKey;

			// Token: 0x0400105D RID: 4189
			public string clipPath;
		}
	}
}
